package com.jingyuan.biz.manager;

import com.google.common.collect.Lists;
import com.jingyuan.biz.event.EventManager;
import com.jingyuan.biz.event.OffLineEvent;
import com.jingyuan.common.entity.ServerMessage;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主机节点管理者
 */
@Service
public class JNodeManager {
    @Resource
    private EventManager eventManager;

    private JNode topNode;
    public JNodeManager(){
        this.topNode=new JNode();
        topNode.setMsg("顶层Node");
    }

    /**
     * parentNode父亲节点，可为null
     * @param parentNode
     */
    public void createNode(JNode parentNode,JNode node){
        if(node==null){
            return;
        }
        if(parentNode==null){
            //node.setParent(topNode);
            topNode.addChildNode(node);
        }else{
            parentNode.addChildNode(node);
        }
    }

    /**
     * 根据通道查找节点
     * @param channel
     * @return
     */
    public JNode getNodeByChannel(Channel channel){
        List<JNode> nodes=topNode.getChildNodes();
        for(JNode node:nodes){
            if(node.getChannel()==channel){
                return node;
            }
        }
        return null;
    }

    public void removeNode(JNode parentNode,JNode node){
        if(parentNode!=null){
            parentNode.removeChildNode(node);
        }else{
            topNode.removeChildNode(node);
        }
    }

    public void removeNode(JNode parentNode,int index){
        if(parentNode!=null){
            parentNode.removeChildNode(index);
        }else{
            topNode.removeChildNode(index);
        }
    }

    public List<JNode> getHostNode(){
        return topNode.getChildNodes();
    }

    /**
     * 移除下线的节点
     */
    public void checkNodeIslive(){
        //超过10秒没有收到心跳包，则移除主机
        topNode.getChildNodes().forEach(node -> {
//            long lastTime = node.getLastKeepLiveTime();
            long nowTime = System.currentTimeMillis();
            boolean isDead=(nowTime-node.getLastKeepLiveTime())>10000;
            if(isDead){
                System.out.println("--------------------主机下线------------------");
                OffLineEvent event=new OffLineEvent();
                event.setActive(false);
                event.setNode(node);
                //分发下线事件
                eventManager.dispatch(event);
            }
        });

//        //有主机被动下线
//        if(!deadNodes.isEmpty()){
//            for(JNode node:topNode.getChildNodes()){
//                Channel channel=node.getChannel();
//                for(JNode deadNode:deadNodes){
//                    ServerMessage message=new ServerMessage();
//                    message.setMsg(String.format("--------------------IP为 %s 的机器被动下线------------------",deadNode.getAddress()));
//                    message.setAddress(deadNode.getAddress());
//                    message.setPort(deadNode.getPort());
//                    channel.writeAndFlush(message);
//                }
//            }
//        }

    }
}
